
<%
if (session.getAttribute("username") == null) {
	%><jsp:forward page="/DBP/login.jsp" /><%
}

String pollid = (String)session.getAttribute("pollid");
String pollname = (String)session.getAttribute("pollName");
String address = (String)request.getParameter("address");
String succ = (String)session.getAttribute("geosuccess");
boolean success = false;
if(address == null){
	address = "unset";
}
if(succ != null){
	success = true;
	session.removeAttribute("geosuccess");
}
String setlat = "-27.15";
String setlong = "150.06";

String sql = "SELECT latitude, longitude FROM Poll WHERE id="+pollid;
try{
	DbConnection db = new DbConnection();
	ResultSet rs = db.getConn().prepareStatement(sql).executeQuery();
	System.out.println(sql);
	if(rs.next()){
		setlat = rs.getString("latitude");
		setlong = rs.getString("longitude");
	}
}catch(Exception e){
	System.out.println(e.toString());
}

String locsuccess = "none";
if(setlat != null && setlong != null){
	System.out.println("SETLAT:"+setlat+"SETLONG:"+setlong);
	locsuccess = "block";
}else{
	System.out.println("SETLONG/LAT UNSET");
	setlat = "-27.15";
	setlong = "150.06";
}

%>
<%@page import="connection.*, java.sql.*" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">  
<head>  
	<meta http-equiv="content-type" name="viewport" content="initial-scale=1.0, user-scalable=no" />  
	 <style type="text/css">
	  html { height: 100% }
	  body { height: 100%; margin: 0; padding: 0 }
	  #map_canvas { height: 100% }
	</style>
 	<link rel="stylesheet" href="/DBP/style/stylesheet.css" type="text/css" />
	<script src="http://maps.googleapis.com/maps/api/js?sensor=true" type="text/javascript"></script>
	<script type="text/javascript">  
		var map;   
		var geocoder;   
		var coordMarker;
		var infoWindow;
		var prevloc;
		var preinfoWindow;
		function initialize() {   
			var latlng = new google.maps.LatLng(<%=setlat%>, <%=setlong%>);
			geocoder = new google.maps.Geocoder();
		    var myOptions = {
		    	zoom: 5,
	      		center: latlng,
		    	mapTypeId: google.maps.MapTypeId.ROADMAP
		    };
		    //show previous location!
		    preinfoWindow = new google.maps.InfoWindow();
		    var blueIcon = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
		    var premyHtml = "<span style=\"display:block;width:300px\"><strong>";
		    if(latlng.lat() != -27.15){
			    premyHtml += "Current location:</strong><br/>Latitude: " +latlng.lat() + "<br/>Longitude: " + latlng.lng() +"</span>";	
		    }else{
		    	premyHtml += "Click the map to select a location, or search for one in the field above!";
		    }
		    var precoordMarker = new google.maps.Marker({
			    map: map,
		        title:"",
		        animation: google.maps.Animation.DROP,
		        icon:blueIcon
		    });
		    infoWindow = new google.maps.InfoWindow();
		    coordMarker = new google.maps.Marker({
		        map: map,
		        title:"",
		        animation: google.maps.Animation.BOUNCE
		    });
			map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
			google.maps.event.addListener(map, "click", function(event) {
				preinfoWindow.setMap(null);
			    var myHtml = "<span style=\"display:block;width:300px\"><strong>New location:</strong><br/>Latitude: " + event.latLng.lat() + "<br/>Longitude: " + event.latLng.lng() +"</span>";	
				infoWindow = new google.maps.InfoWindow({
				        content: myHtml,
				        maxWidth: 1000
			    });
			    coordMarker.setPosition(event.latLng);
			    coordMarker.setMap(map);
			    infoWindow.setPosition(event.latLng);
			    infoWindow.setContent(myHtml);
				infoWindow.open(map,coordMarker);
			    document.getElementById("lati").value = "" + event.latLng.lat();
				document.getElementById("lngi").value = "" +  event.latLng.lng();
		    });
			precoordMarker.setPosition(latlng);
		    precoordMarker.setMap(map);
		    preinfoWindow.setPosition(latlng);
		    preinfoWindow.setContent(premyHtml);
		    preinfoWindow.open(map, precoordMarker);
		}

		function codeAddress() {
		    var address = document.getElementById("address").value;
		    //alert(address);
		    geocoder.geocode( { 'address': address}, function(results, status) {
			    if (status == google.maps.GeocoderStatus.OK) {
				   
					map.setCenter(results[0].geometry.location);
					map.setZoom(15);
					var myHtml = "Latitude: " + results[0].geometry.location.lat() + "<br/>Longitude: " + results[0].geometry.location.lng();	
					infoWindow.setPosition(results[0].geometry.location);
					infoWindow.setContent(myHtml);
					infoWindow.open(map,coordMarker);
					coordMarker.setPosition(results[0].geometry.location);
					coordMarker.setMap(map);	
					document.getElementById("lati").value = latlng.lat();
					document.getElementById("longi").value = latlng.lng();	        
			    } else {
			      //alert("Geocode was not successful for the following reason: " + status);
			    }
			  });
		  }
			/*map = new GMap2(document.getElementById("map_canvas"));   
			map.setCenter(new GLatLng(-33.86890, 151.20709), 11);   
			map.addControl(new GLargeMapControl());   
			geocoder = new GClientGeocoder();   
			
			GEvent.addListener(map,"click", function(overlay,latlng) {
	    	    if (overlay) {
	           		// ignore if we click on the info window
			        return;
			    }
			    var myHtml = "Latitude: " + latlng.lat() + "<br/>Longitude: " + latlng.lng() ;	
			    map.openInfoWindow(latlng, myHtml);
			    document.getElementById("lati").value = latlng.lat();
				document.getElementById("longi").value = latlng.lng();
		        });
			}   */
		
		 
		function addAddressToMap(response) {   
			map.clearOverlays();   
			if (!response || response.Status.code != 200) {   
				alert("Sorry, we were unable to geocode that address");   
			} else {   
				place = response.Placemark[0];   
				point = new GLatLng(place.Point.coordinates[1],   
				place.Point.coordinates[0]);   
				marker = new GMarker(point);   
				map.addOverlay(marker);   
				marker.openInfoWindowHtml('The Place you entered: '+place.address);   
			}   
		}   
		  
		function showLocation() {   
			var address = document.forms[0].q.value;   
			geocoder.getLocations(address, addAddressToMap);   
		}   
		
		function getLat(){
			return latlng.lat();
		}
	</script>
</head>  
  <%//onunload="GUnload()" %>
<body onload="initialize()" >  
<div class="bg">
<div class="content">  
<jsp:include page="/nav.jsp"/>
<div class="pagetitle">Poll management > Poll Location</div>
<div class="entrytitle">
	<form action="#" onsubmit="codeAddress(); return false;">  
		<p>
		Search for an address <%=pollname%>:
		<input type="text" name="q" value="" class="address_input" id="address" size="40" />
		<input type="submit" name="find" class="button" style="display:inline" value="Search" />
		</p>
	</form>  
<div class="entry">
<%if(success){%>
<div class="note" style="display:block;">Success! Poll location set.</div>
<%}
if(locsuccess.equals("block")){%>
<%} %>
	<div id="map_canvas" style="width: auto; min-height: 500px"></div>  	
	<form action="mappingservlet" method="post">
		<input type="hidden" name="lati" value="" id="lati" />
		<input type="hidden" name="lngi" value="" id="lngi" />
		<input type="hidden" name="pollid" value="<%=pollid %>" />
		<input type="submit" onclick="alert(document.getElementById('longi');" class="button" value="Submit" />
	</form>
	</div>
	</div>
</div></div>
</body>  
</html>